﻿namespace WriterStars.Controllers
{
    using System;
    using System.Linq;
    using System.Web.Mvc;
    
    using Microsoft.AspNet.Identity;
    
    using WriterStars.Model;
    using WriterStars.Models.ViewModels;
    using WriterStars.Data;
    using WriterStars.Infrastructure.Services;

    [Authorize]
    public class OrderController : Controller
    {
        private IWriterStarsData data;
        private OrderService orderService;

        public OrderController()
        {
            this.data = new WriterStarsData();
            this.orderService = new OrderService(this.data);
        }

        public ActionResult Create()
        {
            return this.View();
        }

        [HttpPost]
        public ActionResult Create(OrderViewModel orderViewModel)
        {

            var currentUserId = this.User.Identity.GetUserId();

            var order = new Order
            {
                UserId = currentUserId,
                CreationDate = DateTime.Now,
                Keywords = orderViewModel.KeyWords,
                KeywordsDensity = orderViewModel.KeyWordsDensity,
                Description = orderViewModel.Description,
                OrderInfo = orderViewModel.OrderInfo,
                ExpectedPrice = orderViewModel.ExpectedPrice,
                OrderState = OrderState.Pending,
                DueDate = orderViewModel.DueDate,
                NotifyUser = true
            };

            try
            {
                this.data.Orders.Create(order);
                this.data.Orders.SaveChanges();

            }
            catch
            {
                this.ViewBag.Message = "Something went wrong";
            }

            try
            {
                // Not implemented

                var user = this.orderService.GetMostRelevantWriter(order);
                var notification = this.orderService.CreateNotificationForWriter(user);

                this.data.Notifications.Create(notification);
            }
            catch
            {
                this.ViewBag.Message = "Notifying the user is not ready yet";
            }

            return this.RedirectToAction("status", "order", new { @id = order.Id });
        }

        public ActionResult Status(int id)
        {
            var order = this.data.Orders.GetById(id);

            if (order == null)
            {
                return this.RedirectToAction("index", "home");
            }

            return this.View(order);
        }

        [HttpPost]
        public ActionResult ChangeState(int id, OrderState state)
        {
            var order = this.data.Orders.GetById(id);
            if (order == null)
                return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);

            order.OrderState = state;

            this.data.Orders.Update(order);

            return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
        }


        public ActionResult GetForUser()
        {
            var currentUserId = this.User.Identity.GetUserId();

            var orders = this.data.Orders.GetAll().Where(x => x.UserId == currentUserId).ToList();

            return this.PartialView("_UserOrders", orders);
        }

        public ActionResult Delete(int id)
        {
            var currentUserId = this.User.Identity.GetUserId();

            var orders = this.data.Orders.GetAll().Where(x => x.UserId == currentUserId).ToList();

            var toDelete = orders.FirstOrDefault(x => x.Id == id);

            if (toDelete != null)
            {
                this.data.Orders.Delete(toDelete);
                orders.Remove(toDelete);
                this.data.Orders.SaveChanges();
            }

            return this.PartialView("_UserOrders", orders);
        }

    }
}